package com.lanou.wuyou.gift.my;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.wuyou.gift.R;
import com.lanou.wuyou.gift.base.BaseFragment;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.util.Map;

/**
 * Created by dllo on 17/3/17.
 */

public class MyFragment extends BaseFragment{

    private ImageView eye,lock,qq;
    private boolean t;
    private static String NUMBER = "13889631371";
    private static String PASSWORD = "111111";
    private TextView confirm,change,sent;
    private EditText number,password;

    public static MyFragment newInstance(){
        Bundle args = new Bundle();
        MyFragment myFragment = new MyFragment();
        myFragment.setArguments(args);
        return myFragment;
    }
    @Override
    public int setLayout() {
        return R.layout.fragment_my;
    }

    @Override
    public void initView(View view) {
        eye = (ImageView) view.findViewById(R.id.my_eye);
        confirm = (TextView) view.findViewById(R.id.my_confirm);
        password = (EditText) view.findViewById(R.id.my_password);
        number = (EditText) view.findViewById(R.id.my_number);
        change = (TextView) view.findViewById(R.id.my_change);
        lock = (ImageView) view.findViewById(R.id.my_lock);
        sent = (TextView) view.findViewById(R.id.get_verification_code);
        qq = (ImageView) view.findViewById(R.id.my_share_qq);
    }

    @Override
    public void initData() {
        qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

        UMImage image = new UMImage(getActivity(), R.drawable.umeng_socialize_qq);

//        UMShareAPI.get(getContext()).getPlatformInfo(getActivity(), SHARE_MEDIA.QQ, umAuthListener);
        new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.QQ)
                .withText("hello")
                .withMedia(image)
                .setCallback(umShareListener)
                .share();
            }
        });

                password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                eye.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (t) {
                            eye.setImageResource(R.mipmap.close_eye);
                            password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                            password.setSelection(password.getText().length());
                            t = false;
                        } else {
                            eye.setImageResource(R.mipmap.eye);
                            password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                            password.setSelection(password.getText().length());
                            t = true;
                        }
                    }
                });
                confirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (NUMBER.equals(number.getText().toString()) && PASSWORD.equals(password.getText().toString())) {
                            Intent intent = new Intent(getActivity(), MyActivity.class);
                            startActivity(intent);
                        } else if (NUMBER.equals(number.getText().toString()) || PASSWORD.equals(password.getText().toString())) {
                            Toast.makeText(context, "手机号或者密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                eye.setVisibility(View.VISIBLE);
                sent.setVisibility(View.GONE);
                change.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (t) {
                            lock.setImageResource(R.mipmap.lock);
                            password.setText("");
                            password.setHint("输入密码");
                            eye.setVisibility(View.VISIBLE);
                            sent.setVisibility(View.GONE);
                            t = false;
                        } else {
                            lock.setImageResource(R.mipmap.note);
                            password.setText("");
                            password.setHint("短信验证码");
                            eye.setVisibility(View.GONE);
                            sent.setVisibility(View.VISIBLE);
                            password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                            t = true;
                        }
                    }
                });
            }

            private UMShareListener umShareListener = new UMShareListener() {
                @Override
                public void onStart(SHARE_MEDIA platform) {
                    //分享开始的回调
                }

                @Override
                public void onResult(SHARE_MEDIA platform) {
                    Log.d("plat", "platform" + platform);

                    Toast.makeText(getActivity(), platform + " 分享成功啦", Toast.LENGTH_SHORT).show();

                }

                @Override
                public void onError(SHARE_MEDIA platform, Throwable t) {
                    Toast.makeText(getActivity(), platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
                    if (t != null) {
                        Log.d("throw", "throw:" + t.getMessage());
                    }
                }

                @Override
                public void onCancel(SHARE_MEDIA platform) {
                    Toast.makeText(getActivity(), platform + " 分享取消了", Toast.LENGTH_SHORT).show();
                }
            };
            private UMAuthListener umAuthListener = new UMAuthListener() {
                @Override
                public void onStart(SHARE_MEDIA platform) {
                    //授权开始的回调
                }

                @Override
                public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
                    Toast.makeText(getContext(), "Authorize succeed", Toast.LENGTH_SHORT).show();

                }

                @Override
                public void onError(SHARE_MEDIA platform, int action, Throwable t) {
                    Toast.makeText(getContext(), "Authorize fail", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onCancel(SHARE_MEDIA platform, int action) {
                    Toast.makeText(getContext(), "Authorize cancel", Toast.LENGTH_SHORT).show();
                }
            };
}
